//
//  MineOrderTableViewCell.m
//  B2C
//
//  Created by 李小龙 on 2017/7/31.
//  Copyright © 2017年 lixiaolong. All rights reserved.
//

#import "MineOrderTableViewCell.h"

#define OrderButtonBaseTag 123

@implementation MineOrderTableViewCell
#pragma mark - 订单列表
+ (instancetype)mineOrderCellWith:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath count:(NSInteger)count {
    NSInteger index = 0;
    NSString *identifier = @"";
    if (indexPath.row == 0) {
        index = 0;
        identifier = @"mineOrderCellFirst";
    } else if (indexPath.row == count+1) {
        index = 2;
        identifier = @"mineOrderCellThird";
    } else if (indexPath.row == count+2) {
        index = 3;
        identifier = @"mineOrderCellFourth";
    } else {
        index = 1;
        identifier = @"mineOrderCellSecond";
    }
    MineOrderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"MineOrderTableViewCell" owner:self options:nil] objectAtIndex:index];
    }
    return cell;
}
- (void)updateCellWith:(OrderModel *)model indexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        // 时间、状态
        [self.firstTimeLabel setText:[DateSomeThing getDateStrWithTimestampstr:model.createtime formatStr:@"yyyy-MM-dd"]];
        // 状态0已提交，1已付款，2已发货，3已签收，4已评价，5已取消，6已删除，7退款申请，8已退款
        if (model.isrefund) {
            [self.firstStatusLabel setText:@"退款申请"];
        } else {
            switch (model.omstatus.intValue) {
                case 0:
                    [self.firstStatusLabel setText:@"等待买家付款"];
                    break;
                case 1:
                    [self.firstStatusLabel setText:@"等待发货"];
                    break;
                case 2:
                    [self.firstStatusLabel setText:@"已发货"];
                    break;
                case 3:
                    [self.firstStatusLabel setText:@"已完成"];
                    break;
                case 4:
                    [self.firstStatusLabel setText:@"已评价"];
                    break;
                case 5:
                    [self.firstStatusLabel setText:@"已取消"];
                    break;
                case 6:
                    [self.firstStatusLabel setText:@"已删除"];
                    break;
                case 7:
                    [self.firstStatusLabel setText:@"退款申请"];
                    break;
                case 8:
                    [self.firstStatusLabel setText:@"已退款"];
                    break;
                default:
                    break;
            }
        }
        
        
    } else if (indexPath.row == model.orderson.count+1) {
        [self.thirdTotalLabel setText:[NSString stringWithFormat:@"共计%@件商品，合计：",model.omnumber]];
        [self.thirdPriceLabel setText:[NSString stringWithFormat:@"¥%.2f",model.omprice.floatValue]];
    } else if (indexPath.row == model.orderson.count+2) {
        // 状态0已提交，1已付款，2已发货，3已签收，4已评价，5已取消，6已删除，7退款申请，8已退款
        CGFloat buttonY = 9;
        CGFloat buttonW = 75;
        CGFloat kong = 15;
        [self.fourthBackView removeAllSubviews];
        if (model.isrefund) {
            // 查看详情
            OrderHalfButton *button = [OrderHalfButton orderButtonWithType:OrderButtonTypeRefundDetail frame:CGRectMake(ScreenWidth-(kong+buttonW), buttonY, buttonW, self.fourthBackView.height-buttonY*2) buttonTitle:@"查看详情" color:[UIColor blackTextColor] borderColor:[UIColor grayTextColor]];
            [button addTarget:self action:@selector(orderButtonAction:) forControlEvents:UIControlEventTouchUpInside];
            [button setTag:indexPath.section+OrderButtonBaseTag];
            [self.fourthBackView addSubview:button];
        } else {
            switch (model.omstatus.intValue) {
                case 0:
                {
                    for (NSInteger i = 1; i <= 2; i++) {
                        CGRect frame = CGRectMake(ScreenWidth-(kong+buttonW)*i, buttonY, buttonW, self.fourthBackView.height-buttonY*2);// self.fourthBackView.width
                        
                        OrderButtonType type;
                        NSString *buttonTitle;
                        UIColor *color, *borderColor;
                        if (i == 1) {
                            type = OrderButtonTypePay;
                            buttonTitle = @"付款";
                            color = [UIColor orangePriceColor];
                            borderColor = [UIColor orangePriceColor];
                        } else {
                            type = OrderButtonTypeCancel;
                            buttonTitle = @"取消订单";
                            color = [UIColor blackTextColor];
                            borderColor = [UIColor grayTextColor];
                        }
                        
                        OrderHalfButton *button = [OrderHalfButton orderButtonWithType:type frame:frame buttonTitle:buttonTitle color:color borderColor:borderColor];
                        [button addTarget:self action:@selector(orderButtonAction:) forControlEvents:UIControlEventTouchUpInside];
                        [button setTag:indexPath.section+OrderButtonBaseTag];
                        [self.fourthBackView addSubview:button];
                        
                    }
                } break;
                case 1:
                {
                    OrderHalfButton *button = [OrderHalfButton orderButtonWithType:OrderButtonTypeAlert frame:CGRectMake(ScreenWidth-(kong+buttonW), buttonY, buttonW, self.fourthBackView.height-buttonY*2) buttonTitle:@"提醒发货" color:[UIColor orangePriceColor] borderColor:[UIColor orangePriceColor]];
                    [button addTarget:self action:@selector(orderButtonAction:) forControlEvents:UIControlEventTouchUpInside];
                    [button setTag:indexPath.section+OrderButtonBaseTag];
                    [self.fourthBackView addSubview:button];
                } break;
                case 2:
                {
                    for (NSInteger i = 1; i <= 2; i++) {
                        CGRect frame = CGRectMake(ScreenWidth-(kong+buttonW)*i, buttonY, buttonW, self.fourthBackView.height-buttonY*2);// self.fourthBackView.width
                        
                        OrderButtonType type;
                        NSString *buttonTitle;
                        UIColor *color, *borderColor;
                        if (i == 1) {
                            type = OrderButtonTypeSure;
                            buttonTitle = @"确认收货";
                            color = [UIColor orangePriceColor];
                            borderColor = [UIColor orangePriceColor];
                        } else {
                            type = OrderButtonTypeCheck;
                            buttonTitle = @"查看物流";
                            color = [UIColor blackTextColor];
                            borderColor = [UIColor grayTextColor];
                        }
                        
                        OrderHalfButton *button = [OrderHalfButton orderButtonWithType:type frame:frame buttonTitle:buttonTitle color:color borderColor:borderColor];
                        [button addTarget:self action:@selector(orderButtonAction:) forControlEvents:UIControlEventTouchUpInside];
                        [button setTag:indexPath.section+OrderButtonBaseTag];
                        [self.fourthBackView addSubview:button];
                        
                    }
                } break;
                case 3:
                {
                    for (NSInteger i = 1; i <= 3; i++) {
                        CGRect frame = CGRectMake(ScreenWidth-(kong+buttonW)*i, buttonY, buttonW, self.fourthBackView.height-buttonY*2);// self.fourthBackView.width
                        
                        OrderButtonType type;
                        NSString *buttonTitle;
                        UIColor *color, *borderColor;
                        if (i == 1) {
                            type = OrderButtonTypeEvaluate;
                            buttonTitle = @"评价";
                            color = [UIColor orangePriceColor];
                            borderColor = [UIColor orangePriceColor];
                        } else if (i == 2) {
                            type = OrderButtonTypeDelete;
                            buttonTitle = @"删除订单";
                            color = [UIColor blackTextColor];
                            borderColor = [UIColor grayTextColor];
                        } else {
                            type = OrderButtonTypeCheck;
                            buttonTitle = @"查看物流";
                            color = [UIColor blackTextColor];
                            borderColor = [UIColor grayTextColor];
                        }
                        
                        OrderHalfButton *button = [OrderHalfButton orderButtonWithType:type frame:frame buttonTitle:buttonTitle color:color borderColor:borderColor];
                        [button addTarget:self action:@selector(orderButtonAction:) forControlEvents:UIControlEventTouchUpInside];
                        [self.fourthBackView addSubview:button];
                        
                    }
                } break;
                case 4:
                {
                    for (NSInteger i = 1; i <= 2; i++) {
                        CGRect frame = CGRectMake(ScreenWidth-(kong+buttonW)*i, buttonY, buttonW, self.fourthBackView.height-buttonY*2);// self.fourthBackView.width
                        
                        OrderButtonType type;
                        NSString *buttonTitle;
                        UIColor *color, *borderColor;
                        if (i == 1) {
                            type = OrderButtonTypeDelete;
                            buttonTitle = @"删除订单";
                            color = [UIColor orangePriceColor];
                            borderColor = [UIColor orangePriceColor];
                        } else {
                            type = OrderButtonTypeCheck;
                            buttonTitle = @"查看物流";
                            color = [UIColor blackTextColor];
                            borderColor = [UIColor grayTextColor];
                        }
                        
                        OrderHalfButton *button = [OrderHalfButton orderButtonWithType:type frame:frame buttonTitle:buttonTitle color:color borderColor:borderColor];
                        [button addTarget:self action:@selector(orderButtonAction:) forControlEvents:UIControlEventTouchUpInside];
                        [self.fourthBackView addSubview:button];
                        
                    }
                } break;
                case 5:
                {
                    OrderHalfButton *button = [OrderHalfButton orderButtonWithType:OrderButtonTypeDelete frame:CGRectMake(ScreenWidth-(kong+buttonW), buttonY, buttonW, self.fourthBackView.height-buttonY*2) buttonTitle:@"删除订单" color:[UIColor orangePriceColor] borderColor:[UIColor orangePriceColor]];
                    [button addTarget:self action:@selector(orderButtonAction:) forControlEvents:UIControlEventTouchUpInside];
                    [self.fourthBackView addSubview:button];
                } break;
                case 6:
                {
                    
                } break;
                case 7:
                {
                    
                } break;
                case 8:
                {
                    OrderHalfButton *button = [OrderHalfButton orderButtonWithType:OrderButtonTypeDelete frame:CGRectMake(ScreenWidth-(kong+buttonW), buttonY, buttonW, self.fourthBackView.height-buttonY*2) buttonTitle:@"删除订单" color:[UIColor orangePriceColor] borderColor:[UIColor orangePriceColor]];
                    [button addTarget:self action:@selector(orderButtonAction:) forControlEvents:UIControlEventTouchUpInside];
                    [self.fourthBackView addSubview:button];
                } break;
                default:
                    break;
            }
        }
        
    } else {
        OrdersonModel *goodModel = model.orderson[indexPath.row-1];
        
        [self.secondImageView sd_setImageWithURL:GetUrl(goodModel.gimg) placeholderImage:PlaceholderImagePictures];
        [self.secondNameLabel setText:goodModel.gname];
        [self.secondCountLabel setText:[NSString stringWithFormat:@"x%@",goodModel.osnumber]];
        [self.secondPriceLabel setText:[NSString stringWithFormat:@"¥%.2f",goodModel.osprice.floatValue]];
        // 属性
        NSString *specs = @"";
        for (NSInteger i = 0; i < goodModel.otops.count; i++) {
            OtopsModel *otops = goodModel.otops[i];
            if (i == 0) {
                specs = otops.taname;
            } else {
                specs = [NSString stringWithFormat:@"%@;%@",specs,otops.taname];
            }
        }
        [self.secondSpecsLabel setText:specs];
    }
}
- (void)orderButtonAction:(OrderHalfButton *)button {
    if (self.orderBlock) {
        self.orderBlock(button.type, button.tag);
    }
}
#pragma mark - 退款订单列表
+ (instancetype)refundListCellWith:(UITableView *)tableView indexPath:(NSIndexPath *) indexPath count:(NSInteger)count {
    NSInteger index = 0;
    NSString *identifier = @"";
    if (indexPath.row == count) {
        index = 0;
        identifier = @"mineOrderCellFirst";
    } else if (indexPath.row == count+1) {
        index = 3;
        identifier = @"mineOrderCellFourth";
    } else {
        index = 1;
        identifier = @"mineOrderCellSecond";
    }
    MineOrderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"MineOrderTableViewCell" owner:self options:nil] objectAtIndex:index];
    }
    return cell;
}
- (void)updateRefundListCellWith:(OrderModel *)model indexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == model.orderson.count) {
        [self.firstTimeLabel setFont:TextNormalFont12];
        [self.firstTimeLabel setTextColor:[UIColor colorWithHex:0xFF8125 alpha:1.0]];
        [self.firstTimeLabel setText:[NSString stringWithFormat:@"退款金额：%.2f",model.omprice.floatValue]];
        
        [self.firstStatusLabel setFont:TextNormalFont12];
        [self.firstStatusLabel setTextColor:[UIColor colorWithHex:0xFF8125 alpha:1.0]];
        // 状态0已提交，1已付款，2已发货，3已签收，4已评价，5已取消，6已删除，7退款申请，8已退款
        if (model.isrefund) {
            switch (model.refund.firstObject.rstatus.intValue) {
                case 0:
                    [self.firstStatusLabel setText:@"退款中"];
                    break;
                case 1:
                    [self.firstStatusLabel setText:@"已退款"];
                    break;
                case 2:
                    [self.firstStatusLabel setText:@"退款失败"];
                    break;
                case 3:
                    [self.firstStatusLabel setText:@"退款驳回"];
                    break;
                default:
                    [self.firstStatusLabel setText:@"退款申请"];
                    break;
            }
        } else {
            switch (model.omstatus.intValue) {
                case 7:
                    [self.firstStatusLabel setText:@"退款中"];
                    break;
                case 8:
                    [self.firstStatusLabel setText:@"已退款"];
                    break;
                default:
                    break;
            }
        }
        
    } else if (indexPath.row == model.orderson.count+1) {
        CGFloat buttonY = 9;
        CGFloat buttonW = 75;
        CGFloat kong = 15;
        
        OrderHalfButton *button = [OrderHalfButton orderButtonWithType:OrderButtonTypeRefundDetail frame:CGRectMake(ScreenWidth-(kong+buttonW), buttonY, buttonW, self.fourthBackView.height-buttonY*2) buttonTitle:@"查看详情" color:[UIColor blackTextColor] borderColor:[UIColor grayTextColor]];
        [button addTarget:self action:@selector(orderButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [button setTag:indexPath.section+OrderButtonBaseTag];
        [self.fourthBackView addSubview:button];
    } else {
        OrdersonModel *goodModel = model.orderson[indexPath.row];
        
        [self.secondImageView sd_setImageWithURL:GetUrl(goodModel.gimg) placeholderImage:PlaceholderImagePictures];
        [self.secondNameLabel setText:goodModel.gname];
        [self.secondCountLabel setText:[NSString stringWithFormat:@"x%@",goodModel.osnumber]];
        [self.secondPriceLabel setText:[NSString stringWithFormat:@"¥%.2f",goodModel.osprice.floatValue]];
        // 属性
        NSString *specs = @"";
        for (NSInteger i = 0; i < goodModel.otops.count; i++) {
            OtopsModel *otops = goodModel.otops[i];
            if (i == 0) {
                specs = otops.taname;
            } else {
                specs = [NSString stringWithFormat:@"%@;%@",specs,otops.taname];
            }
        }
        [self.secondSpecsLabel setText:specs];
    }
    
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
